popsize = 2;
headsize = 3;
N=90000;

%load %/Users/Alex/Documents/Dev/Ecplise_Workspace/QuasiSpecies/Debug/out.dat;
[head,out] = readData('/Users/Alex/Documents/Dev/Ecplise_Workspace/QuasiSpecies/runMod4_.0046_.0046_.6_.6_.01_1._.7_400_233_90000.dat',headsize,4);
x = linspace(0,max(out(:,1)),max(out(:,1))+1);
y = linspace(0,max(out(:,2)),max(out(:,2))+1);

set(gcf,'PaperType','A4')
%set(gcf,'paperposition',[.25 .25 10.5 8])  

fig1 = figure('name','Quasispecies Development Plots');
colormap(hot);
pops = {zeros(length(y),length(x)),zeros(length(y),length(x))};
for i=1:popsize,
    z = reshape(out(:,2+i),size(y,2),size(x,2));
    pops(i)={z};
    b = ones(size(z))*1.;
    z = z.^b;
    maxelem = max(max(z));
    z = z*(255/maxelem);
    subplot(1,3,i);
    image(x,y,z);
    xlabel('t');
    ylabel('genotype');
    title(['Population ',int2str(i)]) 
end
ax = subplot(1,3,3);
set(ax,'visible','off');
an = annotation(fig1,'textbox',[0.666 0.576 0.15 0.346], 'String',head,'FitHeightToText','on','FontSize',10.);

fig2 = figure('name','Quasispecies Frequency Plots');
tmp = strtrim(head(end));
tmp = textscan(tmp{1}, '%u');
M = tmp{1};
Nstr = head(11);
[start_idx, end_idx] = regexp(Nstr{1},'[0-9]{1,10}');
%N = str2double(Nstr{1}(start_idx:end_idx));

ColOrd = get(gca,'ColorOrder');
[m,n] = size(ColOrd);

for i=1:popsize,
    ax = subplot(2,1,i);
    hold on;
    xlabel('t');
    ylabel('frequency');
    z = reshape(out(:,2+i),size(y,2),size(x,2));
    for j=1:size(M,1)
        
        ColRow = rem(j,m);
        if ColRow == 0
            ColRow = m;
        end
        % Get the color
        Col = ColOrd(ColRow,:);
        
        newy = z(M(j)+1,:)/N;
        plot(ax,x,newy,'DisplayName',['Gene ',num2str(M(j))],'Color',Col);
    end
    
    [a,tmax]=size(z);
    distrWidth=getDistrWidth(z,N)*1/4;
    plot(ax,x,distrWidth,'--r','DisplayName','localization');
    
    title(['Population ',int2str(i)]);
    legend('show');
end

